package com.atguigu.gmall.filter;

import com.atguigu.gmall.common.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/***
 * 自定义网关全局过滤器
 */
@Component
public class GmallFilter implements GlobalFilter, Ordered {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 执行过操作
     *
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取请求对象
        ServerHttpRequest request = exchange.getRequest();
        //获取响应对象
        ServerHttpResponse response = exchange.getResponse();

//        //1 获取用户传来的token
//        //1.1 从url中取token
//        MultiValueMap<String, String> queryParams = request.getQueryParams();
//        String token = queryParams.getFirst("token");
//        if (StringUtils.isEmpty(token)) {
//            //1.2 url中没有token，从请求头中取
//            HttpHeaders headers = request.getHeaders();
//            token = headers.getFirst("token");
//            if (StringUtils.isEmpty(token)) {
//                //1.3 请求头也没token，从cookie中取
//                HttpCookie cookie = request.getCookies().getFirst("token");
//                if (cookie != null) {
//                    token = cookie.getValue();
//                }
//
//            }
//        }
//        //1.4 cookie中也没有token
//        if (StringUtils.isEmpty(token)) {
//            //响应，拒绝请求
//            response.setStatusCode(HttpStatus.VARIANT_ALSO_NEGOTIATES);
//            return response.setComplete();
//        }

        //2 获取到ip地址
        String gatewayIpAddress = IpUtil.getGatwayIpAddress(request);

        //3 根据ip地址，拿到redis中的令牌对比
        String redisToken = stringRedisTemplate.opsForValue().get(gatewayIpAddress);
        if (StringUtils.isEmpty(redisToken)) {
            //redis中没有token或者两边的令牌不相等，拒绝请求
            response.setStatusCode(HttpStatus.VARIANT_ALSO_NEGOTIATES);
            return response.setComplete();
        }

        //放行前把当前用户的token放入请求头中，一对固定的key和value
        request.mutate().header("Authorization","bearer " + redisToken);

        //4 所有错误情况都排除，则放行
        return chain.filter(exchange);
    }

    /**
     * 执行顺序
     *
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
